package com.fg.cloud.factory.controller.order;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.OrderStatus;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.reflection.ParamsReflect;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.order.OrderReturnEntity;
import com.fg.cloud.manager.entry.order.vo.OrderReturnDetailVo;
import com.fg.cloud.manager.entry.order.vo.OrderReturnVo;
import com.fg.cloud.manager.service.order.OrderRefundsService;

/**
 * 
 * Description:退货单
 * @author 陈银龙
 * @date 2017年8月29日上午10:42:40
 */
@Controller
@RequestMapping("factoryOrderRefund")
public class OrderRefundsController extends BaseController{

	@Autowired
	private OrderRefundsService orderRefundsService;
	
	/**
	 * Description:基础查询,关键词（发货单号、订单号、经销商名称、物流公司名称,快递单号）；
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param keyword
	 * @author 陈银龙
	 * @date 2017年8月29日上午10:50:20
	 */
	@RequestMapping("findList")
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, 
			Integer page, Integer rows, String sort, String order, OrderReturnVo entity,
			String keyword) {
		try {
			PageUtils<OrderReturnEntity> pageinfo = 
					new PageUtils<OrderReturnEntity>(page, rows, sort, order);
			FactoryUserVo user = getCurrentFactoryUser();
			entity.setFacId(user.getFacId());
			//反射动态封装查询条件
			Map<String, Object> condition = ParamsReflect.getParamsByReflect(entity);
			pageinfo.setCondition(condition);
			//特殊区域查询处理,封装区域ID，区域编码
			pageinfo.setAgeId(entity.getAreaId());
			pageinfo.setTablename(entity.getAreaNumber() + "_" + entity.getAreaId());
			
			orderRefundsService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	/**
	 * Description:详情查询
	 * @param request
	 * @param response
	 * @param id
	 * @param ageId
	 * @author around
	 * @date 2017年9月6日下午5:24:57
	 */
	@RequestMapping("findDetail")
	@ResponseBody
	public void findDetail(HttpServletRequest request, HttpServletResponse response, 
			Long id, Long ageId){
		try {
			if(null == id){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			OrderReturnEntity entity = orderRefundsService.find(id, 
					getCurrentFactoryUser().getFacId(), ageId);
			doResult(response, true, StatusCode.FINDSUCCESS, entity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	
	/**
	 * 
	 * Description:退货单新增
	 * @param request
	 * @param response
	 * @param dates
	 * @author 陈银龙
	 * @date 2017年8月29日上午11:44:51
	 */
	@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response,String data){
		try {
			if(data == null){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			OrderReturnVo entity = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderReturnVo.class);
			if(entity.getAgeId() == null || entity.getAgeId() < 0 
					|| entity.getExpressCompany() == null
					|| entity.getExpressCompanyNo() == null || entity.getAgeName() == null){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			entity.setStatus(OrderStatus.RETURN_AUDIT);
			entity.setReturnDate(new Date());
			setCreateInfo(entity, request);
			setUpdateInfo(entity, request);
			
			boolean flag = orderRefundsService.insert(entity);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, 
					null, flag ? ResultType.ADD_SUCCESS:ResultType.ADD_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.ADD_FAULT);
		}
	}
	
	/**
	 * Description:退货单修改
	 * @param request
	 * @param response
	 * @param data
	 * @author around
	 * @date 2017年9月6日下午5:29:33
	 */
	@RequestMapping("update")
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response,String data){
		try {
			OrderReturnVo entity = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderReturnVo.class);
			if(entity.getId() == null || entity.getId() < 0 ){
				doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			if(entity.getAgeId() == null || entity.getAgeId() < 0 ){
				doResult(response, false, StatusCode.LACKVALUE, null, "经销商ID为空");
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(entity, request);
			boolean flag = orderRefundsService.update(entity);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, 
					null, flag ? ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.SAVE_FAULT);
		}
	}
	
	/**
	 * Description:批量删除
	 * @param request
	 * @param response
	 * @param ids
	 * @param ageId
	 * @author around
	 * @date 2017年9月6日下午5:31:56
	 */
	@RequestMapping("deletes")
	@ResponseBody
	public void deletes(HttpServletRequest request, HttpServletResponse response, 
			String ids, Long ageId){
		if(null == ids || 0 == ids.length()){
			doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
			return;
		}
		/*if(null == ageId){
			doResult(response, false, StatusCode.LACKVALUE, null, "经销商ID为空");
			return;
		}*/
		
		try {
			boolean flag = orderRefundsService.deletes(LongUtils.convert(ids.split("[, ]+")),
					getCurrentFactoryUser().getFacId(), ageId);
			doResult(response, flag, flag ? StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS:"未查到数据或者已进入流程");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * Description:审核
	 * @param request
	 * @param response
	 * @param entity
	 * @author around
	 * @date 2017年9月6日下午11:57:21
	 */
	@RequestMapping("audit")
	@ResponseBody
	public void audit(HttpServletRequest request, HttpServletResponse response,
			OrderReturnEntity entity){
		if(null == entity.getId()){
			doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
			return;
		}

		try {
			entity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(entity, request);
			boolean flag = orderRefundsService.audit(entity);
			doResult(response, flag, flag ? StatusCode.OPERATIONSUCCESS:StatusCode.OPERATIONERROR, 
					null, flag ? ResultType.OPERATION_SUCCESS:ResultType.OPERATION_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.OPERATIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	
	/**
	 * Description:收货
	 * @param request
	 * @param response
	 * @param entity
	 * @author around
	 * @date 2017年9月6日下午5:36:51
	 */
	@RequestMapping("delivery")
	@ResponseBody
	public void delivery(HttpServletRequest request, HttpServletResponse response, 
			OrderReturnEntity entity){
		if(null == entity.getId()){
			doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
			return;
		}
		
		try {
			entity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(entity, request);
			entity.setReceiveMan(getCurrentFactoryUser().getTrueName());
			entity.setReceiveDate(new Date());
			boolean flag = orderRefundsService.delivery(entity);
			doResult(response, flag, flag ? StatusCode.OPERATIONSUCCESS:StatusCode.OPERATIONERROR, 
					null, flag ? ResultType.OPERATION_SUCCESS:ResultType.OPERATION_FAULT);

		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	/**
	 * Description:退款
	 * @param request
	 * @param response
	 * @param entity
	 * @author around
	 * @date 2017年9月6日下午11:57:01
	 */
	@RequestMapping("refund")
	@ResponseBody
	public void refund(HttpServletRequest request, HttpServletResponse response, 
			OrderReturnEntity entity){
		if(null == entity.getId()){
			doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
			return;
		}
		
		try {
			entity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(entity, request);
			boolean flag = orderRefundsService.refund(entity);
			doResult(response, flag, flag ? StatusCode.OPERATIONSUCCESS:StatusCode.OPERATIONERROR, 
					null, flag ? ResultType.OPERATION_SUCCESS:ResultType.OPERATION_FAULT);

		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	/**
	 * Description:订单详情转退货单详情新增
	 * @param response
	 * @param ids
	 * @author around
	 * @date 2017年9月6日下午11:56:30
	 */
	@RequestMapping("selectReturnDetail")
	@ResponseBody
	public void selectReturnDetail(HttpServletResponse response, String ids){
		try {
			if(null == ids || 0 == ids.length()){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			List<OrderReturnDetailVo> list = orderRefundsService.findByOrderId(
					LongUtils.convert(ids.split("[, ]+")));
			doResult(response, true, StatusCode.OPERATIONSUCCESS, 
					list, ResultType.OPERATION_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}

}
